<template>
	<view class="content">
		<view v-if="cid" class="zy-cid">
			认证失败，请重新认证
		</view>
		<view class="row b-b">
			<text class="tit zy-required">真实姓名</text>
			<input class="input" type="text" v-model="userCert.realName" placeholder="真实姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit zy-required">身份证号</text>
			<input class="input" type="number" v-model="userCert.cardNumber" placeholder="身份证号" placeholder-class="placeholder" />
		</view>
		<!-- <view class="row b-b">
			<text class="tit zy-required">证件类型</text>
			<picker class="subject-type" @change="bindPickerChange" :value="indexSubjectType" :range="subjectType">
				<view class="uni-input">{{subjectType[indexSubjectType]}}</view>
			</picker>
		</view> -->
		<view class="row b-b">
			<text class="tit zy-required">上传身份证正面照</text>
			<view style="width: 100%;">
				<imgUpload :frontBaseUrl="frontBaseUrl" :closeTip="true" :header="header"  :imgCount="uploadImgCount" :uploadImgCount="uploadImgCount" loading async :url="imgUrl" @result="resultUrl" @delImg="delImg"></imgUpload>
			</view>
		</view>
		<view class="row b-b">
			<text class="tit zy-required">上传身份证反面照</text>
			<view style="width: 100%;">
				<imgUpload :frontBaseUrl="frontBaseUrl" :closeTip="true" :header="header"  :imgCount="uploadImgCount" :uploadImgCount="uploadImgCount" loading async :url="imgUrl" @result="resultUrl1" @delImg="delImg1"></imgUpload>
			</view>
		</view>
		<view class="row b-b">
			<text class="tit zy-required">上传手持身份证照</text>
			<view style="width: 100%;">
				<imgUpload :frontBaseUrl="frontBaseUrl" :closeTip="true" :header="header"  :imgCount="uploadImgCount" :uploadImgCount="uploadImgCount" loading async :url="imgUrl" @result="resultUrl2" @delImg="delImg2"></imgUpload>
			</view>
		</view>
		<view class="row b-b">
			<text class="tit zy-required">学生证</text>
			<view style="width: 100%;">
				<imgUpload :frontBaseUrl="frontBaseUrl" :closeTip="true" :header="header"  :imgCount="uploadImgCount" :uploadImgCount="uploadImgCount" loading async :url="imgUrl" @result="resultUrl3" @delImg="delImg3"></imgUpload>
			</view>
		</view>
		<button class="add-btn" @click="confirm" :disabled="submitAddress">提交</button>
	</view>
</template>

<script>
	import zyworkIcon from '@/components/zywork-icon/zywork-icon.vue'
	import * as ResponseStatus from '@/common/response-status.js'
	import imgUpload from '@/components/poiuy-uImgUpload/imgUpload.vue'
	import {
		doGet,
		BASE_URL,
		doPostJson,
		showInfoToast,
		MY_SHARE_CODE,
		SHARE_CODE_PAGE_IMG,
		SHARE_TITLE,
		SHARE_CODE,
		FRONT_BASE_URL,
		LOCAL_FILE_STORAGE,
		USER_TOKEN_KEY,
		isIdentityCard
	} from '@/common/util.js'
	export default {
		components: {
			zyworkIcon,
			imgUpload
		},
		data() {
			return {
				type: 'add',
				userCert: {
					cardImg:'',
					cardImg1:'',
					cardImg2:'',
					cardImg3:'',
					type: 0, //用来存储江西省-赣州市-章贡区
					realName:"",
					cardNumber:""
				},
				subjectType: ['请选择','一卡通', '学生证'],
				indexSubjectType:2,
				submitAddress: false,
				imgUrl:BASE_URL+"/user-certification/user/upload-img",
				frontBaseUrl: FRONT_BASE_URL,
				uploadImgCount:1,
				header:{
					'Authorization': 'Bearer ' + uni.getStorageSync(USER_TOKEN_KEY),
					"App-Auth-appid": "wxxcxappid"
				},
				cid:0,
			}
		},
		onLoad(option){
			if(option.cid){
				this.cid = option.cid
				this.getCefrtifData();
			}
		},
		onBackPress() {
		},
		onUnload() {
		},
		methods: {
			getCefrtifData(){
				doGet("/user-certification/user/info/"+this.cid, {}, {}, true).then(response => {
					let [error, res] = response;
					if (res.data.code === ResponseStatus.OK) {
						 this.userCert = res.data.data;
						 this.indexSubjectType = this.userCert.type
					} else {
						showInfoToast(res.data.message);
					}
				}).catch(err => {
					console.log(err)
				})
			},
			bindPickerChange: function(e) {
				this.indexSubjectType = e.target.value
				this.userCert.type = e.target.value;
			},
			resultUrl(e){
				//成功默认为0
				if(e.code==ResponseStatus.OK){
					this.userCert.cardImg = e.data;
					console.log(this.userCert.cardImg)
				}
			},
			delImg(e){
				this.userCert.cardImg = "";
			},
			resultUrl1(e){
				//成功默认为0
				if(e.code==ResponseStatus.OK){
					this.userCert.cardImg1 = e.data;
					console.log(this.userCert.cardImg1)
				}
			},
			delImg1(e){
				this.userCert.cardImg1 = "";
			},
			resultUrl2(e){
				//成功默认为0
				if(e.code==ResponseStatus.OK){
					this.userCert.cardImg2 = e.data;
					console.log(this.userCert.cardImg2)
				}
			},
			delImg2(e){
				this.userCert.cardImg2 = "";
			},
			resultUrl3(e){
				//成功默认为0
				if(e.code==ResponseStatus.OK){
					this.userCert.cardImg3 = e.data;
					console.log(this.userCert.cardImg3)
				}
			},
			delImg3(e){
				this.userCert.cardImg3 = "";
			},
			//提交
			confirm(){
				let data = this.userCert;
				if(!data.realName){
					showInfoToast('请填写你的姓名');
					return;
				}
				if (!isIdentityCard(data.cardNumber)) {
					showInfoToast('请填写正确的身份证号码')
					return
				}
				if(!data.cardImg){
					showInfoToast('请填上传你的身份证正面照');
					return;
				}
				if(!data.cardImg1){
					showInfoToast('请填上传你的身份证反面照');
					return;
				}
				if(!data.cardImg2){
					showInfoToast('请填上传你的手持身份证照');
					return;
				}
				if(!data.cardImg3){
					showInfoToast('请填上传你的手持身份证照');
					return;
				}
				this.submitAddress = true
				let url = "/user-certification/user/save";
				if(this.cid>0){
					url = "/user-certification/user/update";
				}
				doPostJson(url, data, {}, true).then(response => {
					let [error, res] = response;
					if (res.data.code === ResponseStatus.OK) {
						showInfoToast(res.data.message);
						this.submitAddress = false
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					} else {
						showInfoToast(res.data.message)
						this.submitAddress = false
					}
				}).catch(err => {
					console.log(err)
				})
				
			}
			
		}
	}
</script>

<style lang="scss">
	@import '@/common/zywork-main.scss';
	page{
		background: $page-color-base;
		padding-top: 16upx;
	}
	.zy-cid{
		padding: 30rpx 0rpx;
		width: 100%;
		text-align: center;
		font-size: $font-lg;
		color: #f00000;
	}
	.zy-head-img {
		margin: 10upx auto;
		text-align: center;
	}
	
	.zy-head-img image {
		border-radius: 50%;
		width: 200upx;
		height: 200upx;
	}
	.zy-intro{
		width: 100%;
		padding: 30rpx 30rpx;
		background-color: #FFFFFF;
		.text{
			width: 100%;
			height: 300rpx;
			border: solid 1rpx #e3e3e3;
			border-radius: 15rpx;
			padding: 10rpx 10rpx;
		}
	}
	.business-scope{
		width: 100%;
		height: 150rpx;
		border: solid 1rpx #e3e3e3;
		border-radius: 15rpx;
		padding: 10rpx 10rpx;
	}
	
	.subject-type{
		width: 400rpx;
		height: 50rpx;
		line-height: 50rpx;
	}
	.row{
		display: flex;
		align-items: center;
		position: relative;
		padding:30rpx 30upx;
		background: #fff;
		
		.tit{
			width: 160rpx;
			flex-shrink: 0;
			font-size: 30upx;
			color: $font-color-dark;
		}
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-light;
		}
		.icon-shouhuodizhi{
			font-size: 36upx;
			color: $font-color-light;
		}
	}
	.default-row{
		margin-top: 16upx;
		.tit{
			flex: 1;
		}
		switch{
			transform: translateX(16upx) scale(.9);
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $theme-backcolor;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(0, 170, 127, 0.4);
	}
</style>
